#   Trolls Outta Luckland v0.0.7
#   Copyright 2009-2010, Gummbum
#
#   This file is part of Trolls Outta Luckland.
#
#   Trolls Outta Luckland is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   Trolls Outta Luckland is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Trolls Outta Luckland.  If not, see
#   <http://www.gnu.org/licenses/>.

__version__ = '0.0.7'

### Python imports
import os
from os.path import join as join_path
import sys
import random
from random import randrange as rand

try:
    ### Pygame imports
    import pygame
    ### Game library imports
    import game_globals
    from game_globals import *
    from utils import load_image
except ImportError, e:
    print 'ImportError: %s' % e
    print '    in file "%s"' % os.path.abspath(sys.argv[0])

# Initialized by Sky class constructor
game = None

class Sky(pygame.sprite.LayeredUpdates):
    def __init__(self, max_heavenly=30):
        global game
        game = game_globals.game
        pygame.sprite.LayeredUpdates.__init__(self)
        self.max_heavenly = max_heavenly
        for i in range(self.max_heavenly):
            self.add(Star(randy=True))
    def update(self, *args):
        for i in range(len(self.sprites()), self.max_heavenly):
            if rand(0, 120) == 0:
                self.add(Star())
        pygame.sprite.LayeredUpdates.update(self, *args)

class Star(pygame.sprite.Sprite):
    def __init__(self, *args, **kwargs):
        pygame.sprite.Sprite.__init__(self, *args)
        randy = kwargs.get('randy', False)
        self.image,self.rect = load_image(
            join_path('image','star.png'), -1, False)
        if randy:
            self.y = float(rand(game.window.rect.top, game.window.rect.bottom))
        else:
            self.y = -5.0
        self.rect.center = rand(0,game.window.rect.right), self.y
        speed = rand(0, 10) / 10.0
        self.speed = game.speed() * float(speed+0.1) / 15
        self.alpha_range = [int(round(33.0+speed*128)),]
        self.alpha_range.append(self.alpha_range[0]+64)
        self.alpha = rand(*self.alpha_range)
        self.alpha_step = 0
        self.alpha_target = self.alpha
        self.image.set_alpha(self.alpha)
    def update(self, *args):
        self.y += self.speed
        self.rect.centery = int(self.y)
        if self.y >= game.window.rect.bottom+10:
            self.kill()
        if (self.alpha_step < 0 and self.alpha <= self.alpha_target) or \
        (self.alpha_step >= 0 and self.alpha >= self.alpha_target):
            self.alpha_target = rand(*self.alpha_range)
            if self.alpha < self.alpha_target:
                self.alpha_step = rand(2,8)
            else:
                self.alpha_step = -rand(2,8)
        self.alpha += self.alpha_step
        self.image.set_alpha(self.alpha)
